package com.nidu.demo.notice.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.notice.convertor.NoticeConvertor;
import com.nidu.demo.notice.dto.NoticeCO;
import com.nidu.demo.notice.gateway.NoticeGateway;
import com.nidu.demo.notice.model.Notice;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 通知公告单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NoticeSingleQryExe {

    private final NoticeGateway gateway;
    private final NoticeConvertor convertor;

    public SingleResponse<NoticeCO> execute(Long id) {
        // 1. 根据ID查询邮件账号
        Notice notice = gateway.getById(id);
        if (notice == null) {
            throw new BizException(ErrorCodeConstants.NOTICE_NOT_FOUND.getCode(), ErrorCodeConstants.NOTICE_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        NoticeCO noticeCO = convertor.toClientObject(notice);
        return SingleResponse.of(noticeCO);
    }

}
